import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:syria_sports/presentation/players/add_player_page/widgets/my_circle_avatar.dart';
import 'package:syria_sports/widgets/screen_state_widget.dart';

import '../../../data/controllers/complaints_controller.dart';
import '../../../dialogs/delete_player_confirmation.dart';
import '../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../widgets/app_bar/custom_app_bar.dart';
import '../../../widgets/app_bar/outline_icon_button.dart';
import '../../exercises/exercises_page/models/exercise.dart';
import '../models/complaint.dart';
import 'user_complaints_cubit.dart';
import 'package:flutter/material.dart';


class UserComplaintsScreen extends StatelessWidget {
  const UserComplaintsScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return Scaffold(
      appBar: _buildAppBar(context:context),
      body: BlocProvider<UserComplaintsCubit>(
        create: (context) => UserComplaintsCubit()..init(),
        child: BlocConsumer<UserComplaintsCubit, UserComplaintsState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            var cubit = UserComplaintsCubit.get(context);

            return Container(
              height: height,
              width: width,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(ImageConstant.imgBackground),
                  fit: BoxFit.cover,
                ),
              ),
              child: ScreenStateWidget(
                isLoading: state is UserComplaintsLoading,
                isError: state is UserComplaintsError,
                isDone: state is UserComplaintsDone,
                onRefresh: cubit.getAllNotes,
                onDone: Column(

                  children: [
                    Expanded(
                      child: ListView.builder(
                        itemCount: cubit.notes.length,
                        reverse: true,
                        itemBuilder: (context, index) {
                          final note = cubit.notes[index];
                          return _buildNoteItem(note,context);
                        },
                      ),
                    ),
                    const SizedBox(height: 32),
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  Widget _buildNoteItem(Complaint complaint,BuildContext context) {
    final isMyComplaint = PrefUtils.getUserId() == complaint.user?.userId;
    const String dateTimeFormatPattern = 'yyyy-MM-dd hh:mm';

    return Slidable(
      enabled: isMyComplaint || PrefUtils.isAdmin(),
      endActionPane:!isMyComplaint? null: ActionPane(
        motion: const ScrollMotion(),
        extentRatio: .5,
        children: [
          SlidableAction(
            onPressed: (context){
              showDeleteConfirmation(context, complaint);
            },
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.red,
            icon: Icons.delete,
            label: 'حذف',
          ),

        ],
      ),
      startActionPane:isMyComplaint? null: ActionPane(
        motion: const ScrollMotion(),
        extentRatio: .5,
        children: [
          SlidableAction(
            onPressed: (context){
              showDeleteConfirmation(context, complaint);
            },
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.red,
            icon: Icons.delete,
            label: 'حذف',
          ),

        ],
      ),
      child: Container(
        margin: const EdgeInsets.symmetric(vertical: 0.0, horizontal: 16.0),
        alignment: isMyComplaint ? Alignment.centerLeft : Alignment.centerRight,
        child: Column(
          crossAxisAlignment:
          isMyComplaint ? CrossAxisAlignment.end : CrossAxisAlignment.start,
          children: [
            if(!isMyComplaint)
            const SizedBox(height: 4.0),
            if(!isMyComplaint)
            Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                MyCircleAvatar(
                  width: 12,
                  imageURL: complaint.user?.image,
                ),
                const SizedBox(width: 4),
                Text(
                  (complaint.user?.fullName)??"",
                  style: const TextStyle(
                    color: Colors.white,
                    fontSize: 12.0,
                  ),
                ),
              ],
            ),
            const SizedBox(height: 8.0),
            Container(
              margin:EdgeInsets.only(left:!isMyComplaint?32:0 , right:isMyComplaint?32:0 ),
              padding: const EdgeInsets.all(8.0),
              decoration: BoxDecoration(
                color: isMyComplaint ? Theme.of(context).colorScheme.secondary : Colors.grey[200],
                borderRadius: BorderRadius.circular(8.0),
              ),
              child: Column(
                crossAxisAlignment:
                isMyComplaint ? CrossAxisAlignment.end : CrossAxisAlignment.start,
                children: [
                  Text(
                    complaint.complaint,
                    style: TextStyle(
                      fontSize: 14.0,
                      color: isMyComplaint? Colors.white : Colors.black,
                    ),
                  ),
                  const SizedBox(height: 16.0),
                  Text(
                    '${complaint.createAt?.format(dateTimeFormatPattern)}',
                    style: TextStyle(
                      color: isMyComplaint? Colors.white : Colors.black,
                      fontSize: 12.0,
                    ),
                  ),
                ],
              ),
            ),

          ],
        ),
      ),
    );
  }
  PreferredSizeWidget _buildAppBar({required BuildContext context}) {
    return CustomAppBar(
      leading: OutlineIconButton(
        imagePath: ImageConstant.imgRightIcon,
        onTapped: (){
          Navigator.pop(context);
        },
      ),
      title: AppbarSubtitle(
        text: "الشكاوى", margin: EdgeInsets.only(left: 12.h),
      ),
    );
  }
  void showDeleteConfirmation(BuildContext context, Complaint myGym) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return DeleteConfirmationDialog(
          playerName: (myGym.user?.fullName)??"",
          titleUser: 'شكوى المستخدم',
          onConfirm: () {
            ComplaintsController.deleteById(myGym.complaintId??'-1');
          },
        );
      },
    );
  }
}
